package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;




	/**
	 * 查询所有分类(一二三级分类)
	 * @return 业务数据：[{"categoryId":1,"categoryName":"分类",categoryChild:[..]},{其他1级分类}]
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//1.创建响应结果集合对象-用于封装所有一级分类对象
		List<JSONObject> returnList = new ArrayList<>();
		//2.查询所有分类数据-查询视图即可,共401条记录
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		//3.处理一级分类数据
		//3.1对所有分类集合列表进行分组,按照一级分类id进行分组,得到Map<分组id,以及分组列表>
		Map<Long, List<BaseCategoryView>> category1Map = allCategoryList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
			//3.2封装一级分类对象(一级分类id和分类名称)
			JSONObject jsonObject1 = new JSONObject();
			Long category1Id = entry1.getKey();
			jsonObject1.put("categoryId",category1Id);
			String category1Name = entry1.getValue().get(0).getCategory1Name();
			jsonObject1.put("categoryName",category1Name);

			//4.处理二级分类数据
			List<JSONObject> jsonObject2List = new ArrayList<>();
			//4.1 对一级分类集合按照二级分类id进行分组
			Map<Long, List<BaseCategoryView>> category2Map = entry1.getValue()
					.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//4.2 遍历二级分类Map
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
				//4.3 封装二级分类对象
				JSONObject jsonObject2 = new JSONObject();
				Long category2Id = entry2.getKey();
				jsonObject2.put("categoryId",category2Id);
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				jsonObject2.put("categoryName",category2Name);
				//4.4 将二级分类对象放入二级分类集合中
				jsonObject2List.add(jsonObject2);
				//5.处理三级分类数据
				List<JSONObject> jsonObject3List = new ArrayList<>();
				//5.1 对二级分类列表进行遍历
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					//5.2 封装三级分类对象
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId",baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName",baseCategoryView.getCategory3Name());
					//5.3 将三级分类对象放入集合中
					jsonObject3List.add(jsonObject3);
				}
				//5.4 将三级分类对象集合加入到二级分类对象categoryChild属性中
				jsonObject2.put("categoryChild",jsonObject3List);
			}
			jsonObject1.put("categoryChild",jsonObject2List);
			returnList.add(jsonObject1);
		}
		return returnList;
	}

	/*
	 * 根据一级分类Id获取分类属性以及属性值（标签名，标签值）列表
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
		//获取持久层接口,调用持久层动态SQL
		return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
	}

	/**
	 * 根据一级分类id查询置顶三级分类列表
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> getTopBaseCategory3(Long category1Id) {
		//1.根据一级分类id得到二级分类列表(获取二级分类id列表)
		LambdaQueryWrapper<BaseCategory2> category2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		category2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		category2LambdaQueryWrapper.select(BaseCategory2::getId);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(category2LambdaQueryWrapper);
		if (CollectionUtil.isNotEmpty(baseCategory2List)) {
			//2.根据二级分类id列表得到置顶三级分类
			//2.1 获取所有二级分类id
			List<Long> category2IdList = baseCategory2List
					.stream()
					.map(BaseCategory2::getId)
					.collect(Collectors.toList());
			//2.2 构建 查询三级分类条件
			LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
			baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop,1);
			baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id,category2IdList);
			baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getOrderNum);
			baseCategory3LambdaQueryWrapper.last("limit 7");
			baseCategory3LambdaQueryWrapper.select(BaseCategory3::getId, BaseCategory3::getName, BaseCategory3::getCategory2Id);
			//2.3 查询得到7个置顶三级分类
			List<BaseCategory3> list = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
			return list;
		}
		return null;
	}

	/**
	 * 根据一级分类id查询置顶三级分类列表
	 * @return
	 */
	@Override
	public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
			//1.根据1级分类ID查询分类视图得到“1级”分类列表  封装1级分类对象
			LambdaQueryWrapper<BaseCategoryView> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(BaseCategoryView::getCategory1Id, category1Id);
			List<BaseCategoryView> category1List = baseCategoryViewMapper.selectList(queryWrapper);
			if (CollectionUtil.isNotEmpty(category1List)) {
				String category1Name = category1List.get(0).getCategory1Name();
				//1.1 封装1级分类对象
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("categoryId", category1Id);
				jsonObject1.put("categoryName", category1Name);
				//2.处理二级分类
				List<JSONObject> jsonObject2List = new ArrayList<>();
				//2.1 对"1级"分类列表进行按2级分类ID分组
				Map<Long, List<BaseCategoryView>> map2 = category1List
						.stream()
						.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				//2.2 遍历"2级"分类Map
				for (Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()) {
					Long category2Id = entry2.getKey();
					String category2Name = entry2.getValue().get(0).getCategory2Name();
					//2.3 封装2级分类JSON对象
					JSONObject jsonObject2 = new JSONObject();
					jsonObject2.put("categoryId", category2Id);
					jsonObject2.put("categoryName", category2Name);
					//3.处理三级分类
					List<JSONObject> jsonObject3List = new ArrayList<>();
					for (BaseCategoryView baseCategoryView : entry2.getValue()) {
						JSONObject jsonObject3 = new JSONObject();
						jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
						jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
						jsonObject3List.add(jsonObject3);
					}
					jsonObject2.put("categoryChild", jsonObject3List);
					jsonObject2List.add(jsonObject2);
				}
				//2.4 将2级分类集合存入1级分类categoryChild中
				jsonObject1.put("categoryChild", jsonObject2List);
				return jsonObject1;
			}
			return null;
	}

	/**
	 * 根据三级分类id查询分类视图
	 * @param category3Id
	 * @return
	 */
	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}
}
